package ui;
import java.util.Scanner;
import ctrl.*;
import model.*;
import java.util.*;

public class LoanUI
{
    // instance variables
    private LoanCtrl lCtrl;

    /**
     * Constructor for objects of class LoanUI
     */
    public LoanUI()
    {
        // initialise instance variables
        lCtrl = new LoanCtrl();
    }

    public void Start()
    {
        LoanMenu();
    }

    private void LoanMenu()
    {
        boolean exit = false;
        while(!exit)//that is: while exit is false
        {
            int choice = writeLoanMenu(); 
            switch(choice)
            {
                case 1:
                createLoan();
                break;
                case 2:
                Loan l = findLoan();
                if(l == null)
                {
                    System.out.println("Lån blev ikke fundet!");
                }
                else
                {
                    System.out.println(" Låns id er:" + l.getID() +"\n");
                }           
                break;
                case 3:
                returnLoan();
                break;
                case 4:
                listAllLoans();
                break;
                default:
                exit = true;            
            }
        }
    }

    private int writeLoanMenu()
    {      // makes an object keyboard to read input from the screen
        Scanner keyboard = new Scanner(System.in);
        System.out.println("*** Loan Menu ***");
        System.out.println(" (1) Opret lån");
        System.out.println(" (2) Find lån");
        System.out.println(" (3) Returner lån");
        System.out.println(" (4) List alle lån");
        System.out.println(" (5) Back");
        System.out.print("\n Vaelg: ");
        int choise;
        while(!keyboard.hasNextInt()){
            System.out.println("Indtast venligst et tal ");
            keyboard.next(); 
        }
        choise = keyboard.nextInt();
        System.out.print("\n");
        return choise;
    }

    private String inputLoanID()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Angiv låns id:  ");
        String id = keyboard.nextLine();
        return id;
    }

    private String inputBurrowDate()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Angiv lånedato:  ");
        String borrowDate = keyboard.nextLine();
        return borrowDate;
    }

    private String inputPeriod()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Angiv periode:  ");
        String period = keyboard.nextLine();
        return period;
    }

    private String inputState()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Angiv lånestatus:  ");
        String state = keyboard.nextLine();
        return state;
    }

    private String inputPhone()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Indtast telefonnummer på låner:  ");
        String phone = keyboard.nextLine();
        return phone;
    }

    private String inputSerialNumber()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Angiv kopiens serienummer:  ");
        String serialNumber = keyboard.nextLine();
        return serialNumber;
    }

    private void createLoan()
    {
        String phone = inputPhone();
        String serialNumber = inputSerialNumber();
        Person p = null;
        Copy c = null;
        p = lCtrl.findPerson(phone);

        
        if(p == null || c == null)
        {
            System.out.println("Person eller kopi blev ikke fundet!");
        }
        else
        {
            String id = inputLoanID();
            String borrowDate = inputBurrowDate();
            String period = inputPeriod();
            String state = inputState();

            lCtrl.createLoan(id, borrowDate, period, state, p, c);
        }
    }

    private Loan findLoan()
    {
        Loan l = null;
        String ID = inputLoanID();
        l = lCtrl.findLoan(ID);
        return l;
    }

    private void listAllLoans()
    {
        lCtrl.listAllLoans();
    }

    private void returnLoan()
    {
        String ID = inputLoanID();
        Loan l = lCtrl.findLoan(ID);
        if(l == null)
        {
            System.out.println("Lån blev ikke fundet");
        }
        else
        {
            lCtrl.returnLoan(l);
        }
    }

}
